package com.guigu.community.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.community.pojo.Users;
import com.guigu.community.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Controller
@RequestMapping("/login")
@CrossOrigin
public class UsersController {
    @Autowired
    private UsersService usersService;

    //登录
    @PostMapping("/user")
    @ResponseBody
    public Users login(@RequestBody Users users) {
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("username",users.getUsername());
        Users one = usersService.getOne(wrapper);
        if(users.getPassword().equals(one.getPassword())) {
                return one;}
        return null;
    }

    //注测
    @PostMapping("/register")
    @ResponseBody
    public String register(@RequestBody Users users) {
        users.setIsBusinesses(0);//默认不是为商户
        users.setIsVendor(0);//默认不是为供应商
        users.setCreatedAt(new Date());
        boolean success = usersService.save(users);
        if (success) {
            return "注册成功";
        } else {
            return "注册失败";
        }
    }



    @PutMapping("updateUser")
    @ResponseBody
    public String updateUser(@RequestBody Users users){
        boolean b = usersService.updateById(users);
        if (b)
            return "yes";
        return "no";
    }

    @PostMapping("selectOne/{id}")
    @ResponseBody
    public Users selectOne(@PathVariable Integer id){
        return usersService.getById(id);

    }

    /**
     * 两表相连根据Userid查询
     * 供应商表
     * @param sid
     * @return
     */
    @PostMapping("selectIdSuppliers/{sid}")
    @ResponseBody
    public Users selectIdSuppliers(@PathVariable Integer sid){
        return usersService.listSuppliers(sid);
    }


    /**
     * 两表相连根据Userid查询
     * 商户表
     * @param mid
     * @return
     */
    @PostMapping("selectIdMerchants/{mid}")
    @ResponseBody
    public Users selectIdMerchants(@PathVariable Integer mid){
        return usersService.listMerchants(mid);
    }

}
